package com.clubs.coding.common;

import com.clubs.coding.exception.ImoocMallExceptionEnum;

/*api统一返回的值*/
public class ApiRestResponse<T>{
    private Integer status;
    private String msg;
    private T data;
    private static final int OK_CODE = 200;
    private static final String OK_MSG = "SUCCESS";
    public ApiRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ApiRestResponse(){
        this(OK_CODE,OK_MSG);
    }
    /*
    *
    成功的方法但是不带参
    */
    public static <T> ApiRestResponse<T> success(){
    return new ApiRestResponse<>();
    }
    /*
    *
    成功的方法带参
    */
    public static <T> ApiRestResponse<T> success(T result){
        ApiRestResponse<T> restResponse = new ApiRestResponse<>();
        restResponse.setData(result);
        return restResponse;
    }
    /*
    *
    失败的方法 引用了枚举里面的错误枚举
    */
    public static <T> ApiRestResponse<T> error(ImoocMallExceptionEnum ex){
        return new ApiRestResponse<>(ex.getCode(), ex.getMsg());
    }
    public static <T> ApiRestResponse<T> error(Integer code, String msg) {
        return new ApiRestResponse<>(code, msg);
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
